package com.banksoft.cps.controller.admin;

import com.banksoft.cps.bean.Company;
import com.banksoft.cps.bean.MonitorVideo;
import com.banksoft.cps.bean.query.MonitorVideoQuery;
import com.banksoft.cps.bean.warpper.MonitorVideoWarpper;
import com.banksoft.cps.controller.ControllerConstants;
import com.banksoft.cps.service.CompanyService;
import com.banksoft.cps.service.MonitorVideoService;
import com.banksoft.cps.util.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by rongzhao.zhu on 2018/8/28.
 */
@Controller
@RequestMapping("/admin/monitorVideo/")
public class AdminMonitorVideoController extends ControllerConstants {

    @Autowired
    MonitorVideoService monitorVideoService;
    @Autowired
    CompanyService companyService;
    @RequestMapping(value = "list", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String list(MonitorVideoQuery query){
        List<MonitorVideo> list =  monitorVideoService.list(query);
        return toJSONString(createDataTip(list));
    }
    @RequestMapping(value = "count", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String count(MonitorVideoQuery query){
        long count =  monitorVideoService.count(query);
        long pageCount = PageUtils.getPage(count,query.getSize());
        return toJSONString(createDataTip(pageCount));
    }
    @RequestMapping(value = "find",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String find(String id){
        MonitorVideo bean = monitorVideoService.findById(id);
        return toJSONString(createDataTip(bean));
    }
    @RequestMapping(value = "findWarpper",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String findWarpper(String id){
        MonitorVideo bean = monitorVideoService.findById(id);
        MonitorVideoWarpper warpper = new MonitorVideoWarpper();

        if(bean !=null){
            BeanUtils.copyProperties(bean,warpper);

        }else {
            warpper = new MonitorVideoWarpper();
        }
        Company company = companyService.findById(bean.getCompanyId());
        warpper.setbLatitude(company.getbLatitude());
        warpper.setbLongitude(company.getbLongitude());
        warpper.seteLatitude(company.geteLatitude());
        warpper.seteLongitude(company.geteLongitude());
        warpper.setMapImage(company.getMapImage());
        return toJSONString(createDataTip(warpper));
    }
    @RequestMapping(value = "save", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String save(MonitorVideo bean, HttpServletRequest request){
        try {
            monitorVideoService.save(bean);
            return toJSONString(SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return toExceptionString(e);
        }
    }

    @RequestMapping(value = "delete", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String save(String  id, HttpServletRequest request){
        try {
            monitorVideoService.delete(id);
            return toJSONString(SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return toExceptionString(e);
        }
    }


}
